// pages/profile-message/profile-message.js
const app = getApp()
const constant = require("../../constant.js")
const storage = require("../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    begin: 0,
    length: 10,
    principalMessages: [],
    more: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      qiniu: constant.resource.qiniu,
      host: constant.api(app.globalData.pord).url,
      mobile: wx.getStorageSync(storage.keys.mobile)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.listMessage();
  },

  listMessage: function() {
    wx.showLoading({
      title: "正在加载中..."
    })
    var that = this;

    wx.request({
      url: that.data.host + '/coach/profile/messages?mobile=' + that.data.mobile + '&begin=' + that.data.begin + '&length=' + that.data.length,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          for (let i in res.data.data.principalMessages) {
            that.data.principalMessages.push(res.data.data.principalMessages[i]);
          }
          if (res.data.data.principalMessages.length < that.data.length) {
            that.setData({
              more: false
            })
          }
          that.setData({
            principalMessages: that.data.principalMessages,
            begin: that.data.begin + that.data.length
          });
          wx.hideLoading();
        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function () {
        wx.hideLoading();
        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.more) {
      this.listMessage();
    }
  },

  bindReadMessages: function(e) {
    this.setData({
      newsId: e.currentTarget.dataset.id
    })
    var index = e.currentTarget.dataset.index;
    //
    if(e.currentTarget.dataset.read != "true") {
       // 请求提交
      var messageId = this.data.newsId;
      var that = this;
      // let unionid = wx.getStorageSync(storage.keys.unionid);
      wx.request({
        url: that.data.host + '/coach/profile/messages/' + messageId + "?mobile=" + that.data.mobile,
        method: 'POST',
        dataType: 'json',
        // header: { "Content-Type": "application/x-www-form-urlencoded" },
        responseType: 'text',
        success: function (res) {
          if (res.data.code == "0") {
            that.data.principalMessages[index].read = true;
            that.setData({
              principalMessages: that.data.principalMessages,
            });
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 1000
            })
          }
        },
        fail: function (res) { }
      })
    }
  },

  bindPullUp: function(e) {
    this.setData({
      newsId: null
    })
  }
})